package com.yulongtian.offers.day202301;

/**
 * https://leetcode.cn/problems/trapping-rain-water/
 * 可
 * 接雨水 双指针方法
 *
 * @author yulongTian
 * @create 2023-01-09 14:19
 */
public class Test02 {
    public static void main(String[] args) {
        int[] height = {4, 2, 0, 3, 2, 5};
        System.out.println(trap(height));
    }

    public static int trap(int[] height) {
        int ans = 0;
        int left = 0;
        int len = height.length;
        int right = len - 1;

        int pre_max = height[0];
        int latter_max = height[len - 1];

        while (left < right) {
            if (pre_max <= latter_max) {
                left++;
                ans += pre_max - height[left - 1];
                pre_max = Math.max(pre_max, height[left]);
            } else {
                right--;
                ans += latter_max - height[right + 1];
                latter_max = Math.max(latter_max, height[right]);
            }
        }
        return ans;
    }

}
